package com.example.warehouse_manager.service.impl;

import com.example.warehouse_manager.entity.ProductType;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.entity.Role;
import com.example.warehouse_manager.mapper.ProductTypeMapper;
import com.example.warehouse_manager.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Yang
* @description 针对表【product_type(商品分类表)】的数据库操作Service实现
* @createDate 2024-03-22 17:05:17
*/
@Service
@CacheConfig(cacheNames = "com.example.warehouse_manager.service.impl.ProductTypeServiceImpl")
public class ProductTypeServiceImpl implements ProductTypeService{

    @Autowired
    private ProductTypeMapper productTypeMapper;

    //查询所有商品分类树
    @Override
    @Cacheable(key = "'all:typeTree'")
    public List<ProductType> productTypeTree() {
        //查出所有的商品分类
        List<ProductType> allProductTypeList = productTypeMapper.findAllProductType();
        //将所有商品分类转成商品分类树
        return allTypeToTypeTree(allProductTypeList, 0);
    }


    //将所有商品分类转成商品分类树的递归算法
    private List<ProductType> allTypeToTypeTree(List<ProductType> typeList,Integer pid){
        //拿到所有一级分类
        List<ProductType> firstLevelType =new ArrayList<>();
        for (ProductType productType : typeList) {
            if (productType.getParentId().equals(pid)){
                firstLevelType.add(productType);
            }
        }

        //查出每一个一级分类下的所有二级分类
        for (ProductType productType : firstLevelType) {
            List<ProductType> secondLeveTypes = allTypeToTypeTree(typeList, productType.getTypeId());
            productType.setChildProductCategory(secondLeveTypes);
        }

        return firstLevelType;
    }


    //校验分类编码或分类名称是否存在业务方法
    @Override
    public Result checkTypeCode(String typeCode) {
        //判断
        ProductType productType = new ProductType();
        productType.setTypeCode(typeCode);
        ProductType typeByCodeOrName = productTypeMapper.findTypeByCodeOrName(productType);

        return Result.ok(typeByCodeOrName==null);
    }

    //添加商品分类
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result saveProductType(ProductType productType) {
        //检验分类名称是否已经存在
        ProductType prodType = new ProductType();
        prodType.setTypeName(productType.getTypeName());
        ProductType prodCategory = productTypeMapper.findTypeByCodeOrName(prodType);
        if (prodCategory!=null){
            return Result.err(Result.CODE_ERR_BUSINESS,"分类名称已存在");
        }

        //添加分类
        int i = productTypeMapper.insertProductType(productType);
        if(i>0){
            return Result.ok("商品分类添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"商品分类添加失败");
    }

    //删除商品分类
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result deleteProductType(Integer typeId) {
        int i = productTypeMapper.removeProductType(typeId);
        if (i>0){
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！");
    }

    //修改商品分类
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result setProductType(ProductType productType) {
        //查询修改的分类名称是否已存在
        ProductType productType1 = new ProductType();
        productType1.setTypeName(productType.getTypeName());

        ProductType prodCategory = productTypeMapper.findTypeByCodeOrName(productType1);

        if (prodCategory != null && !prodCategory.getTypeId().equals(productType.getTypeId())) {
            return Result.err(Result.CODE_ERR_BUSINESS,"商品分类已存在");
        }

        //修改
        int i = productTypeMapper.setProductTypeById(productType);
        if (i>0){
            return Result.ok("商品分类修改成功！");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"商品分类修改失败");
    }


}




